<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/3/26
 * Time: 8:33
 * Desc: 商品原料管理
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Material extends BasicAdmin{
    /**
     * 原材料管理
     * @return [type] [description]
     * 2018-03-26
     */
    public function index(){
        $this->title = '原材料分类';
        $get = $this->request->get();
        $map = array();
        $map['a.is_deleted'] = array('eq',0);
        if(!empty($get['name'])){
            $this->assign('name',$get['name']);
            $map['a.name'] = array('eq',$get['name']);
        }else{
            $this->assign('name',0);
        }
        $rows = intval($this->request->get('rows', cookie('rows')));
        cookie('rows', $rows >= 10 ? $rows : 20);
        // if(!empty($get['warning'])&&($get['warning']==1)){  //库存充足
        // 	$this->assign('warning',1);
        // 	$page = Db::name('goods_material')->alias('a')->join('ys_goods_material_category b','a.cat_id=b.id','LEFT')->where($map)->where('a.stock>a.warn_stock')->field('a.*,b.name as category')->paginate($rows, false, ['query' => request()->get('', '', 'urlencode')]);
        // }elseif(!empty($get['warning'])&&($get['warning']==2)){		//库存不充足
        // 	$this->assign('warning',2);
        // 	$page = Db::name('goods_material')->alias('a')->join('ys_goods_material_category b','a.cat_id=b.id','LEFT')->where($map)->where('a.stock<a.warn_stock')->field('a.*,b.name as category')->paginate($rows, false, ['query' => request()->get('', '', 'urlencode')]);
        // }else{
        // 	$this->assign('warning',0);
        // }
        $page = Db::name('goods_material')->alias('a')->join('ys_goods_material_category b','a.cat_id=b.id','LEFT')->where($map)->field('a.*,b.name as category')->paginate($rows, false, ['query' => request()->get('', '', 'urlencode')]);
        list($pattern, $replacement) = [['|href="(.*?)"|', '|pagination|'], ['data-open="$1"', 'pagination pull-right']];
        list($result['list'], $result['page']) = [$page->all(), preg_replace($pattern, $replacement, $page->render())];
        $num = 0;
        foreach($result['list'] as $k => $v){
            if($v['stock'] < $v['warn_stock'])
                $num++;
        }
        $this->assign('title',$this->title);			//必须加上title,不然顶部的按钮不会显示
        $this->assign('num',$num);
        return $this->fetch('', $result);
    }
    /**
     * 添加原材料
     */
    public function add_material(){
        $table = "GoodsMaterial";
        $list = Db::name('goods_material_category')->select();
        $this->assign('time',time());
        $this->assign('list',$list);
        return $this->_form($table, 'add_material');
    }

    /**
     * 删除原材料
     * @return [type] [description]
     */
    public function del_material(){
        $table = "GoodsMaterial";
        if (DataService::update($table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
    /**
     * 原材料分类
     * @return [type] [description]
     */
    public function material_category(){
        $this->title = '原材料分类';
        $get = $this->request->get();
        $db = Db::name('goods_material_category')->where('is_deleted',0);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db);
    }
    /**
     * 添加 -- 编辑原材料分类
     */
    public function add_material_category(){
        $table = "GoodsMaterialCategory";
        $this->assign('time',time());
        return $this->_form($table, 'add_material_category');
    }
    /**
     * 删除原材料分类
     * @return [type] [description]
     */
    public function del_material_category(){
        $table = "GoodsMaterialCategory";
        if (DataService::update($table)) {
            $this->success("分类删除成功！", '');
        }
        $this->error("分类删除失败，请稍候再试！");
    }
}